<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="终端编码" prop="terminalCode">
        <el-input
          v-model="queryParams.terminalCode"
          placeholder="请输入终端编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="终端名称" prop="terminalName">
        <el-input
          v-model="queryParams.terminalName"
          placeholder="请输入终端名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="终端类型" prop="terminalType">
        <el-select v-model="queryParams.terminalType" placeholder="请选择终端类型" clearable>
          <el-option
            v-for="dict in kmx_terminal_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="IP地址" prop="ipAddress">
        <el-input
          v-model="queryParams.ipAddress"
          placeholder="请输入IP地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最后登录时间" style="width: 308px">
        <el-date-picker
          v-model="daterangeLastLogin"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in kmx_device_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['shebeiguanli:terminal:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['shebeiguanli:terminal:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['shebeiguanli:terminal:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['shebeiguanli:terminal:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="terminalList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="终端ID" align="center" prop="terminalId" />
      <el-table-column label="终端编码" align="center" prop="terminalCode" />
      <el-table-column label="终端名称" align="center" prop="terminalName" />
      <el-table-column label="终端类型" align="center" prop="terminalType">
        <template #default="scope">
          <dict-tag :options="kmx_terminal_type" :value="scope.row.terminalType"/>
        </template>
      </el-table-column>
      <el-table-column label="MAC地址" align="center" prop="macAddress" />
      <el-table-column label="IP地址" align="center" prop="ipAddress" />
      <el-table-column label="认证令牌" align="center" prop="authToken" />
      <el-table-column label="最后登录时间" align="center" prop="lastLogin" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.lastLogin, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="kmx_device_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['shebeiguanli:terminal:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['shebeiguanli:terminal:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改终端设备对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="terminalRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="终端编码" prop="terminalCode">
          <el-input v-model="form.terminalCode" placeholder="请输入终端编码" />
        </el-form-item>
        <el-form-item label="终端名称" prop="terminalName">
          <el-input v-model="form.terminalName" placeholder="请输入终端名称" />
        </el-form-item>
        <el-form-item label="终端类型" prop="terminalType">
          <el-select v-model="form.terminalType" placeholder="请选择终端类型">
            <el-option
              v-for="dict in kmx_terminal_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="MAC地址" prop="macAddress">
          <el-input v-model="form.macAddress" placeholder="请输入MAC地址" />
        </el-form-item>
        <el-form-item label="IP地址" prop="ipAddress">
          <el-input v-model="form.ipAddress" placeholder="请输入IP地址" />
        </el-form-item>
        <el-form-item label="认证令牌" prop="authToken">
          <el-input v-model="form.authToken" placeholder="请输入认证令牌" />
        </el-form-item>
        <el-form-item label="最后登录时间" prop="lastLogin">
          <el-date-picker clearable
            v-model="form.lastLogin"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择最后登录时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in kmx_device_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Terminal">
import { listTerminal, getTerminal, delTerminal, addTerminal, updateTerminal } from "@/api/shebeiguanli/terminal";

const { proxy } = getCurrentInstance();
const { kmx_terminal_type, kmx_device_status } = proxy.useDict('kmx_terminal_type', 'kmx_device_status');

const terminalList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeLastLogin = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    terminalCode: null,
    terminalName: null,
    terminalType: null,
    ipAddress: null,
    lastLogin: null,
    status: null,
  },
  rules: {
    terminalCode: [
      { required: true, message: "终端编码不能为空", trigger: "blur" }
    ],
    terminalName: [
      { required: true, message: "终端名称不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "状态不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询终端设备列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeLastLogin && '' != daterangeLastLogin) {
    queryParams.value.params["beginLastLogin"] = daterangeLastLogin.value[0];
    queryParams.value.params["endLastLogin"] = daterangeLastLogin.value[1];
  }
  listTerminal(queryParams.value).then(response => {
    terminalList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    terminalId: null,
    terminalCode: null,
    terminalName: null,
    terminalType: null,
    macAddress: null,
    ipAddress: null,
    authToken: null,
    lastLogin: null,
    status: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("terminalRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeLastLogin.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.terminalId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加终端设备";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _terminalId = row.terminalId || ids.value
  getTerminal(_terminalId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改终端设备";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["terminalRef"].validate(valid => {
    if (valid) {
      if (form.value.terminalId != null) {
        updateTerminal(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addTerminal(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _terminalIds = row.terminalId || ids.value;
  proxy.$modal.confirm('是否确认删除终端设备编号为"' + _terminalIds + '"的数据项？').then(function() {
    return delTerminal(_terminalIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('shebeiguanli/terminal/export', {
    ...queryParams.value
  }, `terminal_${new Date().getTime()}.xlsx`)
}

getList();
</script>
